<?php
/**
 * 字典库
 * User: lxl
 * Date: 2020/07/22
 */

namespace App\Service\Core\Sys\Dict;

use App\Model\Core\Sys\SysDictModel;
use App\Service\ParamsService;
use App\Service\Core\CoreBaseService;
use App\Traits\ModelStoreTrait;
use Illuminate\Support\Arr;

/**
 * @method  $this setDictItem($val)
 * @method  $this setDictSubVal($val)
 * @method  $this setDictSubValExt($val)
 * @method  $this setSortOrder($val)
 * @method  $this setStatus($val)
 */
class SysDictStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    //必传参数
    private array $required_param = ['dict_item', 'dict_sub_val', 'dict_sub_val_ext', 'sort_order'];

    public function store($id = 0): array
    {
        //接收验证参数
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) return alert_info(1, $this->errors[0], $this->errors);

        //过滤参数
        $data_param = ['dict_item', 'dict_sub_val', 'dict_sub_val_ext', 'sort_order', 'status'];
        $other_param = [];
        $all_param = Arr::collapse([$data_param, $other_param]);
        foreach ($this->params as $key => $value) {
            if (easy_in_array($key, $all_param)) $$key = $value;
        }

        //初始data数据
        $data = [];
        foreach ($data_param as $value) {
            if (easy_in_array($value, $all_param)) $data[$value] = isset($$value) ? $$value : '';
        }

        //验证信息
        if (!Arr::exists(ParamsService::$dict_item_list, $dict_item)) return alert_info(1, '字典项不存在');

        //判断重复字典
        $check = SysDictModel::getInstance()->checkIdByDictSubVal($dict_sub_val, $id);
        if (!empty($check)) return alert_info(1, '字典信息已存在(字典子值重复)');

        //开启事务
        $connection = SysDictModel::getInstance()->getConnection();
        $connection->beginTransaction();

        //判断更新OR新增
        if ($id) {
            //验证字典信息
            if (empty(SysDictService::getInstance()->getOne($id))) {
                $connection->rollBack();
                return alert_info(1, '修改信息不存在');
            }

            //更新字典
            $data['status'] = $status;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = SysDictModel::getInstance()->updateOne($id, $data);
            $this->addLog('字典管理', '修改字典【' . $data['dict_sub_val'] . '】', $this->operate_id, $this->operate_by);
        } else {
            //新增字典
            $data['status'] = ParamsService::STATUS_ENABLE;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = SysDictModel::getInstance()->addOne($data);
            $id = $res;
            $this->addLog('字典管理', '添加字典【' . $data['dict_sub_val'] . '】', $this->operate_id, $this->operate_by);
        }

        if ($res === false) {
            $connection->rollBack();
            return alert_info(1, '操作失败:数据库繁忙，请稍后再试');
        }

        //提交事务
        $connection->commit();
        return alert_info(0, '操作成功', ['id' => $id]);
    }

}
